package com.xqpaymall.service;

import com.xqpaymall.common.domain.vo.RequestVo;
import com.xqpaymall.common.domain.vo.ResponseVo;
import com.xqpaymall.common.enumm.RetEnum;
import com.xqpaymall.common.util.JwtTokenUtils;
import com.xqpaymall.dal.dao.mapper.UserInfoMapper;
import com.xqpaymall.dal.dao.model.UserInfo;
import com.xqpaymall.dal.dao.model.UserInfoExample;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @ Author     ：marui
 * @ Date       ：Created in 9:52 2019/10/16
 */
@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    public ResponseVo login(RequestVo loginVo){
        UserInfoExample userInfoExample = new UserInfoExample();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andUsernameEqualTo(loginVo.getUserName());
        criteria.andPasswordEqualTo(loginVo.getPassWord());
        List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);
        ResponseVo responseVo = new ResponseVo();
        if(userInfos.size() == 0){
            // 失败
            responseVo.setCode(RetEnum.RET_ERROR_USERNAME_PASSWORD.getCode());
            responseVo.setMessage(RetEnum.RET_ERROR_USERNAME_PASSWORD.getMessage());
        }else{
            // 成功
            HashMap<String, Object> map = new HashMap<>();
            map.put("uid",userInfos.get(0).getId());
            // 当前时间加3600秒，即1小时
            map.put("exp", DateTime.now().plusSeconds(3600).toDate().getTime()/1000);
            responseVo.setCode(RetEnum.RET_SUCCESS.getCode());
            responseVo.setMessage(RetEnum.RET_SUCCESS.getMessage());
            responseVo.setToken(JwtTokenUtils.generatorToken(map));
        }
        return  responseVo;
    }

}
